package com.logistic.server.controller;

import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.entity.common.PageRequest;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.pojo.SysDictData;
import com.logistic.server.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 字典数据 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "字典数据接口", tags = {"字典数据接口"})
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    @Resource
    private ISysDictDataService dictDataService;

    /**
    * 字典数据列表
    * @param dictData 字典数据
    * @param pageRequest 分页参数
    * @return Result
    */
    @ApiOperation(value = "字典数据列表", notes = "字典数据列表")
    @GetMapping
    public Result<?> list(SysDictData dictData, PageRequest pageRequest) {
        return ResultGenerator.genSuccessResult(dictDataService.selectPageDictDataList(dictData, pageRequest));
     }

    /**
    * 字典数据新增
    * @param dictData 字典数据
    * @return Result
    */
    @ApiOperation(value = "字典数据新增", notes = "字典数据新增")
    @PostMapping
    @LoginRequired
    public Result<?> add(@RequestBody SysDictData dictData) {
        dictDataService.insertDictData(dictData);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典数据删除
    * @param ids 字典数据主键
    * @return Result
    */
    @ApiOperation(value = "字典数据删除", notes = "字典数据删除")
    @DeleteMapping("/{ids}")
    @LoginRequired
    public Result<?> delete(@PathVariable Long[] ids) {
        dictDataService.deleteDictData(ids);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典数据修改
    * @param dictData 字典数据
    * @return Result
    */
    @ApiOperation(value = "字典数据修改", notes = "字典数据修改")
    @PutMapping
    @LoginRequired
    public Result<?> update(@RequestBody SysDictData dictData) {
        dictDataService.updateDictData(dictData);
        return ResultGenerator.genSuccessResult();
    }

    /**
    * 字典数据详情
    * @param id 字典数据主键
    * @return Result
    */
    @ApiOperation(value = "字典数据详情", notes = "字典数据详情")
    @GetMapping("/{id}")
    public Result<?> detail(@PathVariable Long id) {
        return ResultGenerator.genSuccessResult(dictDataService.selectDictDataById(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     * @param dictType 类型
     * @return 结果
     */
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public Result<?> dictType(@PathVariable String dictType) {
        return ResultGenerator.genSuccessResult(dictDataService.selectDictDataByType(dictType));
    }
}
